﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace DataLayer
{
    public class IconsHelper : SystemFileHelper
    {
        private static List<SystemFile> listIcon = null;
        public override List<SystemFile> LoadAll()
        {
             listIcon = new List<SystemFile>();
            XmlDocument document = new XmlDocument();
            XmlTextReader reader = new XmlTextReader("./SystemFile/Icons.xml");
            reader.Read();
            // load reader
            document.Load(reader);
            for (int i = 0; i < document.ChildNodes[0].ChildNodes.Count; i++)
            {
                Icon ico = new Icon();
                XmlNode currentNode = document.ChildNodes[0].ChildNodes[i];
                ico.Name = currentNode.SelectSingleNode("Name").InnerText;
                ico.Id = int.Parse(currentNode.SelectSingleNode("ResourceID").InnerText);
                //Lack of Resource Loader.
                
                string path = ResourcesHelper.GetInstance().IDToPath(ico.Id);
                ico.Bitmap = new BitmapImage(new Uri(path));
                listIcon.Add(ico);
            }
            return listIcon;
        }

        public override void Update()
        {
            base.Update();
        }

        public Icon GetIconById(int id)
        {
            Icon rs = new Icon(id);
            if (listIcon == null)
            {
                this.LoadAll();
            }
            int count = listIcon.Count;
            for (int i = 0; i < count; i++)
            {
                if (((Icon)listIcon[i]).Id == id)
                {
                    rs = ((Icon)listIcon[i]);
                }
            }
            return rs;
        }
    }
}
